var seerRole = require("./werewolfRole/seer.js")
var witchRole = require("./werewolfRole/witch.js")
var hunterRole = require("./werewolfRole/hunter.js")
var idiotsRole = require("./werewolfRole/idiots.js")
var villagerRole =require("./werewolfRole/villager.js")
var werewolfRole = require("./werewolfRole/werewolf.js")
//狼人杀
module.exports.createRoom = function(roomId,channelService,GameRemote) {
	console.log("狼人杀new!!55555创建房间:"+roomId)
	var gameBasic = require("./basic/gameBasic.js")
	var local = {}
	//身份定义
	var roleClass = {				//protoss   神民   villager   村民   werewolf 狼阵营
		"seer" : seerRole,      	//"预言家"
		"witch" : witchRole, 		//"女巫"
		"hunter" : hunterRole,		//"猎人"
		"idiots" : idiotsRole,		//"白痴"
		"villager" : villagerRole, 	//"村民"
		"werewolf" : werewolfRole	//"狼人"
	}
	//本局身份
	// var roleDeploy = ["seer","witch","hunter","idiots","villager","villager","villager","villager","werewolf","werewolf","werewolf","werewolf"]
	var roleDeploy = ["seer","witch","hunter","idiots","villager","werewolf"]
	var playerNum = 6
	var room = new gameBasic(roomId,"lrs",playerNum,channelService,GameRemote)
	//添加解散模块
	var dissolution = require("./basic/dissolution.js")
	dissolution.call(room)
	room.roleMap = {} //角色对应椅子号列表
	room.roleList = {} //角色列表

	room.nextTime = false
	room.nextFun = [] 					//方法栈

	room.KONGXIAN_STATE = 0 			//空闲阶段
	room.NONE_STATE = -1 				//不可操作阶段
	room.PRE_STAET = 1 					//预开始阶段
	room.NIGHT_WEREWOLF_STATE = 2       //夜晚狼人阶段
	room.NIGHT_SEER_STATE = 3        	//夜晚预言家阶段
	room.NIGHT_WITCH_STATE = 4        	//夜晚女巫阶段
	room.NIGHT_HUNTER_STATE = 5        	//夜晚猎人阶段
	room.DAY_RUNSHERIFF_JOIN = 6     	//竞选警徽上警阶段
	room.DAY_RUNSHERIFF_SAY1 = 7		//竞选警徽发言阶段
	room.DAY_RUNSHERIFF_VOTE = 8     	//竞选警徽投票阶段
	room.DAY_RUNSHERIFF_SAY2 = 9		//竞选警徽发言阶段
	room.DAY_ANNOUNCE_RESULT = 10 		//公布夜晚结果阶段
	room.SHERIFF_CHANGE = 11 			//警徽移交阶段
	room.LAST_WORDS = 12 				//首日遗言阶段
	room.DAY_HUNTER_READY = 13 			//猎人等待开枪状态
	room.DAY_PLAYER_SAY_READY = 14 		//准备开始放逐发言阶段
	room.DAY_PLAYER_SAY_1 = 15   		//第一轮放逐发言
	room.DAY_BANISH_VOTE = 16 			//放逐投票阶段
	room.DAY_PLAYER_SAY_2 = 17          //第二轮放逐发言
	

	room.state = room.KONGXIAN_STATE 	//当前状态
	room.victoryType = "killSide"  		//胜利类型  屠边 killSide 屠城  killAll

	room.nightWerewolfTime = 30000    	//狼人杀人时间
	room.nightSeerTime = 30000 			//预言家验人时间
	room.nightWitchTime = 30000  		//女巫操作时间
	room.nightHunterTime = 30000 		//猎人查看时间
	room.runSheriffJoinTime = 10000     //上警时间
	room.runSheriffJoinVoteTime = 20000 //警长投票时间
	room.banishVoteTime = 15000 		//放逐投票时间
	room.playerTime = 15000 			//玩家操作时间
	room.defaultTime = 2000 			//默认时间

	room.day = 1 						//天数
	room.diurnal = "day" 		        //白天黑夜状态
	room.houseOwner = -1 				//房主uid
	room.sheriffChair = -1 				//警长椅子号
	room.oldSheriffChair = -1 			//旧警长椅子号
	room.killList = {} 					//狼人猎杀目标列表
	room.killTarget = false 			//狼人最终猎杀目标
	room.witchAction = {"type" : false,"target" : false} //女巫操作
	room.joinRunSheriffList = {} 		//参与警长竞选的玩家列表
	room.voteList = {} 					//投票列表
	room.votedList = {} 				//被投票列表
	room.wantRole = {} 					//期望身份列表
	//主循环
	room.mainUpdate = function(dt) {
		if(room.nextTime !== false && room.nextFun.length){
			if(room.nextTime <= 0){
				room.nextTime = false
				if(room.nextFun.length){
					var fun = room.nextFun.shift()
					fun()
				}
			}else{
				room.nextTime -= dt
			}
		}
	}
	//开始游戏，初始化、发身份牌
	room.gameBegin = function() {
		room.state = room.PRE_STAET
		//随机分配身份
		roleDeploy.sort(function(a,b) {return Math.random() < 0.5 ? 1 : -1})
		for(var i in room.wantRole){
			for(var j = 0;j < roleDeploy.length;j++){
				if(roleDeploy[j] == room.wantRole[i]){
					//交换位置
					var tmp = roleDeploy[i]
					roleDeploy[i] = roleDeploy[j]
					roleDeploy[j] = tmp
				}
			}
		}
		console.log(roleDeploy)
		room.roleMap = {}
		room.day = 1
		room.sheriffChair = -1
		//发放身份牌
		for(var i = 0;i < roleDeploy.length;i++){
			if(!room.roleMap[roleDeploy[i]]){
				room.roleMap[roleDeploy[i]] = []
			}
			room.roleMap[roleDeploy[i]].push(i)
			room.roleList[i] = new roleClass[roleDeploy[i]](room,i)
			if(room.players[i] && room.players[i].isActive){
				var notify = {
					cmd : "roleCard",
					roleInfo : room.roleList[i].getRoleInfo()
				}
				room.sendByUid(room.players[i].uid,notify)
			}
		}
	}
	//角色操作
	room.handle.roleAction = function(uid,sid,param,cb) {
		var chair = room.chairMap[uid]
		var type = param.type
		var target = param.target
		room.roleList[chair].action(type,target,cb)
	}
	//房主操作
	room.handle.houseAction = function(uid,sid,param,cb) {
		if(uid != room.houseOwner){
			cb(false,"房主才能操作")
			return
		}
		var type = param.type
		switch(type){
			case "startGame":
				//开始游戏
				if(room.state !== room.PRE_STAET){
					cb(false,"不在预开始阶段")
					return
				}
				cb(true)
				room.startGame()
			break
			case "beginSheriffVote":
				//开始警上投票
				if(room.state == room.DAY_RUNSHERIFF_SAY1 || room.state == room.DAY_RUNSHERIFF_SAY2){
					room.runSheriffVote()
					cb(true)
				}else{
					cb(false)
				}
			break
			case "finishLastWords":
				//结束遗言
				if(room.state != room.LAST_WORDS){
					cb(false,"不在遗言阶段")
					return
				}
				cb(true)
				room.nextTime = 0
			break
			case "beginBanishVote":
				//开始放逐投票
				if(room.state == room.DAY_PLAYER_SAY_1 || room.state == room.DAY_PLAYER_SAY_2){
					room.banishVote()
					cb(true)
				}else{
					cb(false)
				}
			break
			default:
				cb(false,"type error : "+type)
		}
	}
	//游戏操作
	room.handle.gameAction = function(uid,sid,param,cb) {
		var chair = room.chairMap[uid]
		var type = param.type
		var target = param.target
		switch(type){
			case "runSheriffJoin":
				//竞选警长
				if(room.state != room.DAY_RUNSHERIFF_JOIN){
					cb(false,"不在竞选警长阶段")
					return
				}
				var chair = room.chairMap[uid]
				if(!room.joinRunSheriffList[chair]){
					room.joinRunSheriffList[chair] = true
					cb(true)
				}else{
					cb(false,"已报名")
				}
			break
			case "quitElection":
				//退水
				if(room.state != room.DAY_RUNSHERIFF_SAY1 && room.state != room.DAY_RUNSHERIFF_SAY2){
					cb(false,"不在警上发言阶段")
					return
				}
				var chair = room.chairMap[uid]
				if(room.joinRunSheriffList[chair] !== true){
					cb(false,"未竞选")
					return
				}
				room.joinRunSheriffList[chair] = false
				room.sendAll({cmd : "quitElection", chair : chair})
				cb(true)
			break
			case "sheriffVote":
				//警上投票  未竞选可以投票
				if(room.state != room.DAY_RUNSHERIFF_VOTE){
					cb(false,"不在警长投票阶段")
					return
				}
				var chair = room.chairMap[uid]
				if(room.roleList[chair].survive && room.roleList[chair].votePower && room.joinRunSheriffList[chair] == undefined){
					if(typeof(target) == "number" && room.joinRunSheriffList[target] == true && room.voteList[chair] == undefined){
						room.voteList[chair] = target
						cb(true)
					}else{
						cb(false)
					}
				}else{
					cb(false)
				}
			break
			case "banishVote":
				//放逐投票 存活且有投票权可以投票
				if(room.state != room.DAY_BANISH_VOTE){
					cb(false,"不在放逐投票阶段")
					return
				}
				var chair = room.chairMap[uid]
				if(room.roleList[chair].survive && room.roleList[chair].votePower){
					if(typeof(target) == "number" && room.voteList[chair] == undefined 
						&& room.roleList[chair].survive && room.roleList[chair].showRole != "survive-idiots"){
						room.voteList[chair] = target
						cb(true)
					}else{
						cb(false)
					}
				}else{
					cb(false)
				}
			break
			case "sheriffChange":
				//移交警徽
				if(room.state != room.SHERIFF_CHANGE){
					cb(false,"不在警徽移交阶段")
					return
				}
				var chair = room.chairMap[uid]
				if(room.oldSheriffChair != chair){
					cb(false,"非警长")
					return
				}
				var target = param.target
				if(typeof(target) != "number" || !room.roleList[target].survive){
					cb(false,"target error : "+target)
					return
				}
				room.sheriffChair = target
				cb(true)
				room.nextTime = 0
			break
			default:
				cb(false,"type error : "+type)
		}
	}
	//================================================游戏流程===============================//
	//游戏开始
	room.startGame = function() {
		var notify = {
			cmd : "startGame"
		}
		room.sendAll(notify)
		room.enterNight()
	}
	room.enterNight = function() {
		room.diurnal = "night"
		room.sendAll({cmd : "enterNight"})
		room.nightWerewol()
	}
	//夜晚狼人阶段
	room.nightWerewol = function() {
		room.state = room.NIGHT_WEREWOLF_STATE
		room.killList = {}
		room.killTarget = false
		room.sendAll({cmd : "werewolfState"})
		var notify = {
			cmd : "werewolfAction",
			werewols : room.roleMap["werewolf"],
			time : room.nightWerewolfTime
		}
		room.sendByRole("werewolf",notify)
		room.nextTime = room.nightWerewolfTime + 1000
		room.nextFun.unshift(room.nightWerewolKillend)
	}
	//狼人刀人结束
	room.nightWerewolKillend = function() {
		var target = false
		var maxNum = 0
		var targetList = {}
		for(var i in room.killList){
			if(!targetList[room.killList[i]]){
				targetList[room.killList[i]] = 0
			}
			targetList[room.killList[i]]++
		}
		for(var i in targetList){
			if(targetList[i] > maxNum){
				target = i
				maxNum = targetList[i]
			}
		}
		room.killTarget = target
		room.nightSeer()
	}
	//夜晚预言家阶段
	room.nightSeer = function() {
		room.state = room.NIGHT_SEER_STATE
		room.sendAll({cmd : "seerState"})
		room.nextFun.unshift(room.nightWitch)
		if(!room.roleList[room.roleMap["seer"][0]].survive){
			room.nextTime = Math.floor(Math.random() * room.defaultTime) + room.defaultTime
		}else{
			room.roleList[room.roleMap["seer"][0]].state = true
			var notify = {
				cmd : "seerAction",
				time : room.nightSeerTime
			}
			room.sendByRole("seer",notify)
			room.nextTime = room.nightSeerTime + 1000
		}
	}
	//夜晚女巫阶段
	room.nightWitch = function() {
		room.state = room.NIGHT_WITCH_STATE
		room.witchAction = {"type" : false,"target" : false}
		room.sendAll({cmd : "witchState"})
		room.nextFun.unshift(room.nightHunter)
		if(!room.roleList[room.roleMap["witch"][0]].survive){
			room.nextTime = Math.floor(Math.random() * room.defaultTime) + room.defaultTime
		}else{
			room.roleList[room.roleMap["witch"][0]].state = true
			var notify = {
				cmd : "witchAction",
				time : room.nightWitchTime
			}
			if(room.roleList[room.roleMap["witch"][0]].save){
				notify.killTarget = room.killTarget
			}else{
				notify.killTarget = -1
			}
			room.sendByRole("witch",notify)
			room.nextTime = room.nightWitchTime + 1000
		}
	}
	//夜晚猎人阶段
	room.nightHunter = function() {
		room.state = room.NIGHT_HUNTER_STATE
		room.sendAll({cmd : "hunterState"})
		room.nextFun.unshift(room.enterDay)
		var chair = room.roleMap["hunter"][0]
		if(!room.roleList[chair].survive){
			room.nextTime = Math.floor(Math.random() * room.defaultTime) + room.defaultTime
		}else{
			var notify = {
				cmd : "hunterAction",
				time : room.nightHunterTime
			}
			if(room.witchAction["type"] == "poison" && room.witchAction["target"] == chair){
				notify.state = false
			}else{
				notify.state = true
			}
			room.sendByRole("hunter",notify)
			room.nextTime = room.nightHunterTime
		}
	}
	//进入白天
	room.enterDay = function() {
		room.state = room.NONE_STATE
		room.day++
		room.diurnal = "day"
		room.sendAll({cmd : "enterDay"})
		//加入放逐发言阶段
		room.nextFun.unshift(room.banishSpeak1)
		if(room.day == 2){
			//第二天白天竞选警徽
			room.runSheriffJoin()
		}else{
			//其余直接宣布结果
			room.announceResult()
		}
	}
	//报名竞选警徽阶段
	room.runSheriffJoin = function() {
		room.state = room.DAY_RUNSHERIFF_JOIN
		room.sendAll({cmd : "runSheriffJoin",time : room.runSheriffJoinTime})
		room.joinRunSheriffList = {}
		room.nextFun.unshift(room.runSheriffSay1)
		room.nextTime = room.runSheriffJoinTime
	}
	//警上第一轮发言阶段
	room.runSheriffSay1 = function() {
		room.state = room.DAY_RUNSHERIFF_SAY1
		//随机一个玩家开始发言
		var tmpList = []
		for(var i in room.joinRunSheriffList){
			tmpList.push(i)
		}
		if(tmpList.length == 1){
			room.confirmSheriff(tmpList[0])
		}else if(tmpList.length){
			var rand = tmpList[Math.floor(Math.random() * tmpList.length)]
			room.nextFun.unshift(room.runSheriffEnd1)
			room.sendAll({cmd : "runSheriffSay1",rand : rand,joinRunSheriffList : room.joinRunSheriffList})
		}else{
			room.sheriffDrain()
		}
	}
	//警上第一轮投票结束
	room.runSheriffEnd1 = function() {
		var maxTarget = -1
		var maxVoteCount = 0
		var tmpVoteList = {}
		var notify = {
			cmd : "runSheriffEnd",
			voteList : room.voteList
		}
		room.sendAll(notify)
		//记录投票人数
		for(var i in room.voteList){
			if(!tmpVoteList[room.voteList[i]]){
				tmpVoteList[room.voteList[i]] = 0
			}
			tmpVoteList[room.voteList[i]]++
		}
		for(var i in tmpVoteList){
			if(tmpVoteList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = tmpVoteList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、警徽流失
			room.sheriffDrain()
		}else{
			var tmpCount = 0
			for(var i in tmpVoteList){
				if(tmpVoteList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，第二轮发言
				for(var i in room.joinRunSheriffList){
					if(tmpVoteList[i] < maxVoteCount){
						delete room.joinRunSheriffList[i]
					}
				}
				room.runSheriffSay2()
			}else{
				//选出警长
				room.confirmSheriff(maxTarget)
			}
		}
	}
	//竞选警长第二轮发言阶段
	room.runSheriffSay2 = function() {
		room.state = room.DAY_RUNSHERIFF_SAY2
		//随机一个玩家开始发言
		var tmpList = []
		for(var i in room.joinRunSheriffList){
			tmpList.push(room.joinRunSheriffList[i])
		}
		var rand = tmpList[Math.floor(Math.random() * tmpList.length)]
		room.nextFun.unshift(room.runSheriffEnd2)
		room.sendAll({cmd : "runSheriffSay2",rand : rand,joinRunSheriffList : room.joinRunSheriffList})
	}
	//警上第二轮投票结束
	room.runSheriffEnd2 = function() {
		var maxTarget = -1
		var maxVoteCount = 0
		var tmpVoteList = {}
		var notify = {
			cmd : "runSheriffEnd",
			voteList : room.voteList
		}
		room.sendAll(notify)
		//记录投票人数
		for(var i in room.voteList){
			if(!tmpVoteList[room.voteList[i]]){
				tmpVoteList[room.voteList[i]] = 0
			}
			tmpVoteList[room.voteList[i]]++
		}
		for(var i in tmpVoteList){
			if(tmpVoteList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = tmpVoteList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、警徽流失
			room.sheriffDrain()
		}else{
			var tmpCount = 0
			for(var i in tmpVoteList){
				if(tmpVoteList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，警徽流失
				room.sheriffDrain()
			}else{
				//选出警长
				room.confirmSheriff(maxTarget)
			}
		}
	}
	//警上投票阶段
	room.runSheriffVote = function() {
		room.state = room.DAY_RUNSHERIFF_VOTE
		room.voteList = {}
		room.sendAll({cmd : "runSheriffVote",time : room.runSheriffJoinVoteTime,joinRunSheriffList : room.joinRunSheriffList})
		room.nextTime = room.runSheriffJoinVoteTime
	}
	//警徽流失
	room.sheriffDrain = function() {
		room.state = room.NONE_STATE
		room.sheriffChair = -1
		room.sendAll({cmd : "sheriffDrain"})
		room.nextFun.unshift(room.announceResult)
		room.nextTime = room.defaultTime
	}
	//选出警长
	room.confirmSheriff = function(chair) {
		room.state = room.NONE_STATE
		room.sheriffChair = chair
		room.sendAll({cmd : "confirmSheriff", sheriffChair : room.sheriffChair})
		room.nextFun.unshift(room.announceResult)
		room.nextTime = room.defaultTime
	}
	//宣布夜晚结果
	room.announceResult = function() {
		room.state = room.DAY_ANNOUNCE_RESULT
		var flag = false
		var dielist = {}
		if(room.killTarget !== false && room.witchAction.type !== "save"){
			dielist[room.killTarget] = "werewolf"
			flag = true
		}
		if(room.witchAction.type == "poison"){
			dielist[room.witchAction.target] = "poison"
			flag = true
		}
		if(flag){
			//先执行死亡操作
			for(var i in dielist){
				room.dieDispose(i,dielist[i])
				dielist[i] = true
			}
			if(room.day == 2 && room.state == room.NONE_STATE){
				//首夜有人死亡进入遗言阶段
				room.nextFun.unshift(room.lastWordsStage.bind(this,dielist))
			}
		}
		for(var i in dielist){
			dielist[i] = true
		}
		room.sendAll({cmd : "announceResult", dielist : dielist})
		room.nextTime = room.defaultTime
	}
	//角色退场处理阶段
	room.dieDispose = function(chair,reason) {
		room.roleList[chair].survive = false
		switch(reason){
			case "poison":
				//被毒
			case "werewolf":
				//被刀
				//均不显示死亡信息
				room.roleList[chair].showRole = "die-none"
			break
			case "banish":
				//被放逐
				if(room.roleList[chair].role == "idiots"){
					//白痴翻牌不死
					room.roleList[chair].survive = true
					room.roleList[chair].votePower = false
					room.roleList[chair].showRole = "survive-idiots"
				}else{
					room.roleList[chair].showRole = "die-banish"
				}
			break
			case "die-hunter":
				//猎人翻牌
			break
			case "gun":
				//被开枪
				room.roleList[chair].showRole = "die-gun"
			break
			case "boom":
				//狼人自爆
				room.roleList[chair].showRole = "die-boom"
			break
		}
		//警长死亡移交警徽
		if(!room.roleList[chair].survive && room.sheriffChair == chair){
			room.nextFun.unshift(room.sheriffChangeStage)
		}
		//猎人死亡进入猎人阶段
		if(room.roleList[chair].role == "hunter"){
			var state = true
			if(reason == "poison"){
				state = false
			}
			if(state){
				//可以开枪则进入开枪状态
				room.nextFun.unshift(room.hunterStage)
			}
		}
		var notify = {
			cmd : "dieDispose",
			chair : chair,
			showRole : room.roleList[chair].showRole
		}
		room.sendAll(notify)
		//自动进入下一阶段
		room.nextTime = 0
		//判断胜负
		if(!room.roleList[chair].survive){
			if(!room.victoryCheck()){
				room.state = room.KONGXIAN_STATE
				return
			}
		}
	}
	//猎人开枪阶段
	room.hunterStage = function() {
		room.state = room.DAY_HUNTER_READY
		room.sendByUid(room.players[room.roleMap["hunter"][0]].uid,{cmd : "hunterStage"})
		room.nextTime = room.playerTime
	}
	//移交警徽阶段
	room.sheriffChangeStage = function() {
		room.state = room.SHERIFF_CHANGE
		var notify = {
			cmd : "sheriffChangeStage",
			time : room.playerTime
		}
		room.sendAll(notify)
		room.oldSheriffChair = room.sheriffChair
		room.sheriffChair = -1
		room.nextFun.unshift(room.sheriffChangeEnd)
		room.nextTime = room.playerTime
	}
	//警徽移交结束
	room.sheriffChangeEnd = function() {
		room.state = room.NONE_STATE
		room.oldSheriffChair = -1
		var notify = {
			cmd : "sheriffChangeEnd",
			sheriffChair : room.sheriffChair
		}
		room.sendAll(notify)
		room.nextTime = room.defaultTime
	}
	//遗言阶段
	room.lastWordsStage = function(dielist) {
		room.state = room.LAST_WORDS
		var notify = {
			cmd : "lastWordsStage",
			dielist : dielist
		}
		room.sendAll(notify)
	}
	//第一轮放逐发言阶段
	room.banishSpeak1 = function() {
		room.state = room.DAY_PLAYER_SAY_1
		room.nextFun.unshift(room.banishend1)
		var notify = {
			cmd : "banishSpeak1"
		}
		room.sendAll(notify)
	}
	//第一轮放逐投票结束
	room.banishend1 = function() {
		room.state = room.NONE_STATE
		var maxTarget = -1
		var maxVoteCount = 0
		var notify= {
			cmd : "banishend1",
			voteList : room.voteList
		}
		room.sendAll(notify)
		//记录投票人数
		for(var i in room.voteList){
			if(!room.votedList[room.voteList[i]]){
				room.votedList[room.voteList[i]] = 0
			}
			room.votedList[room.voteList[i]] += 1
			if(i == room.sheriffChair){
				room.votedList[room.voteList[i]] += 0.5
			}
		}
		for(var i in room.votedList){
			if(room.votedList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = room.votedList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、无人出局进入黑夜
			room.banishFinish(false)
		}else{
			var tmpCount = 0
			for(var i in room.votedList){
				if(room.votedList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，第二轮发言
				for(var i in room.votedList){
					if(room.votedList[i] < maxVoteCount){
						delete room.votedList
					}
				}
				room.nextFun.unshift(room.banishSpeak2)
				room.nextTime = room.defaultTime
			}else{
				//被放逐玩家死亡、进入遗言阶段
				room.banishFinish(maxTarget)
			}
		}
	}
	//第二轮放逐发言阶段
	room.banishSpeak2 = function() {
		room.state = room.DAY_PLAYER_SAY_2
		room.nextFun = room.banishend2
		var notify = {
			cmd : "banishSpeak2"
		}
		room.sendAll(notify)
	}
	//第二轮放逐投票结束
	room.banishend2 = function() {
		room.state = room.NONE_STATE
		var maxTarget = -1
		var maxVoteCount = 0
		var notify= {
			cmd : "banishend2",
			voteList : room.voteList
		}
		room.sendAll(notify)
		//记录投票人数
		for(var i in room.voteList){
			if(!room.votedList[room.voteList[i]]){
				room.votedList[room.voteList[i]] = 0
			}
			room.votedList[room.voteList[i]] += 1
			if(i == room.sheriffChair){
				room.votedList[room.voteList[i]] += 0.5
			}
		}
		for(var i in room.votedList){
			if(room.votedList[i] > maxVoteCount){
				maxTarget = i
				maxVoteCount = room.votedList[i]
			}
		}
		if(maxTarget == -1){
			//无人投票、无人出局进入黑夜
			room.banishFinish(false)
		}else{
			var tmpCount = 0
			for(var i in room.votedList){
				if(room.votedList[i] >= maxVoteCount){
					tmpCount++ 
				}
			}
			if(tmpCount > 1){
				//多人平票，无人出局，进入黑夜
				for(var i in room.votedList){
					if(room.votedList[i] < maxVoteCount){
						delete room.votedList
					}
				}
				room.banishFinish(false)
			}else{
				//被放逐玩家死亡、进入遗言阶段
				room.banishFinish(maxTarget)
			}
		}
	}
	//放逐投票阶段
	room.banishVote = function() {
		room.state = room.DAY_BANISH_VOTE
		room.voteList = {}
		room.sendAll({cmd : "banishVote",time : room.banishVoteTime})
		room.nextTime = room.banishVoteTime
	}
	//放逐投票完成
	room.banishFinish = function(chair) {
		console.log("room.nextTime ： ",room.nextTime)
		var notify = {
			cmd : "banishFinish",
			chair : chair
		}
		room.sendAll(notify)
		if(chair !== false){
			room.nextFun.unshift(room.enterNight)
			var dielist = {}
			dielist.chair = true
			room.nextFun.unshift(room.lastWordsStage.bind(this,dielist))
			room.dieDispose(chair,"banish")
		}else{
			room.nextFun.unshift(room.enterNight)
		}
		room.nextTime = room.defaultTime
	}
	//胜负条件判断
	room.victoryCheck = function(){
		var camps = {
			"werewolf" : 0,
			"protoss" : 0,
			"villager" : 0
		}
		for(var i in room.roleList){
			if(room.roleList[i].survive){
				if(camps[room.roleList[i].camp] != undefined){
					camps[room.roleList[i].camp]++
				}
			}
		}
		if(room.victoryType == "killSide"){
			//屠边
			if(camps["werewolf"] == 0){
				//好人胜利
				local.settlement("villager")
				return true
			}else if(camps["protoss"] == 0 || camps["villager"] == 0){
				//狼人胜利
				local.settlement("werewolf")
				return true
			}
		}else if(room.victoryType == "killAll"){
			//屠城
			if(camps["werewolf"] == 0){
				//好人胜利
				local.settlement("villager")
				return true
			}else if(camps["protoss"] + camps["villager"] == 0){
				//狼人胜利
				local.settlement("werewolf")
				return true
			}
		}
		return false
	}
	//游戏结束
	local.settlement = function(camp) {
		room.nextFun = []
		var notify = {
			cmd : "settlement",
			camp : camp,
			roleList : {}
		}
		for(var i in room.roleList){
			notify.roleList[i] = room.roleList[i].getRoleInfo()
		}
		room.sendAll(notify)
		room.gameSettlement()
	}
	//房间结束
	local.gameOverBefore = function(){
		console.log("游戏结束")
		room.gameOver()
	}
	//==========================================================//
	room.setBeforeInfo = function(info) {
		if(info && info.wantRole && typeof(info.wantRole) == "object"){
			for(var i in info.wantRole){
				room.wantRole[i] = info.wantRole[i]
			}
		}
	}
	room.joinLaterFun = function(chair) {
		if(room.houseOwner == -1){
			console.log("room.houseOwner ： "+room.players[chair].uid)
			room.houseOwner = room.players[chair].uid
		}
  	}
  	//向某个身份的存活玩家发消息
	room.sendByRole = function(role,notify) {
		console.log(room.roleMap)
		if(room.roleMap[role]){
			for(var i = 0;i < room.roleMap[role].length;i++){
				var chair = room.roleMap[role][i]
				console.log(chair)
				console.log(room.roleList[chair])
				if(room.roleList[chair].survive && room.players[chair] && room.players[chair].isActive){
					room.sendByUid(room.players[chair].uid,notify)
				}
			}
		}
	}
	room.handle.getRoomInfo = function(uid,sid,param,cb) {
		var chair = room.chairMap[uid]
		var info = {
			roomType : room.roomType,
			roomId : room.roomId,
			chair : chair,
			victoryType : room.victoryType,
			players : room.getPlayersInfo(),
			state : room.state,
			playerNum : playerNum,
			nextTime : room.nextTime,
			day : room.day,
			diurnal : room.diurnal,
			houseOwner : room.houseOwner,
			sheriffChair : room.sheriffChair,
			joinRunSheriffList : room.joinRunSheriffList,
			voteList : room.voteList,
			votedList : room.votedList,
			roleList : {}
		}
		for(var i in room.roleList){
			if(i == chair){
				info.roleList[i] = room.roleList[i].getRoleInfo()
			}else{
				info.roleList[i] = room.roleList[i].getPublicInfo()
			}
		}
		cb(true,info)
	}
	return room
}